package org.springframework.data.relational.core.dialect;

import org.springframework.data.relational.core.sql.LockOptions;

public class SQLiteDialect extends AbstractDialect {
	/**
	 * Singleton instance.
	 */
	public static final SQLiteDialect INSTANCE = new SQLiteDialect();

	protected SQLiteDialect() {
	}

	private static final LimitClause LIMIT_CLAUSE = new LimitClause() {

		/*
		 * (non-Javadoc)
		 * 
		 * @see
		 * org.springframework.data.relational.core.dialect.LimitClause#getLimit(long)
		 */
		@Override
		public String getLimit(long limit) {
			return "LIMIT " + limit;
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see
		 * org.springframework.data.relational.core.dialect.LimitClause#getOffset(long)
		 */
		@Override
		public String getOffset(long offset) {
			return "OFFSET " + offset;
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see
		 * org.springframework.data.relational.core.dialect.LimitClause#getClause(long,
		 * long)
		 */
		@Override
		public String getLimitOffset(long limit, long offset) {
			return String.format("LIMIT %d OFFSET %d", limit, offset);
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see org.springframework.data.relational.core.dialect.LimitClause#
		 * getClausePosition()
		 */
		@Override
		public Position getClausePosition() {
			return Position.AFTER_ORDER_BY;
		}
	};

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.data.relational.core.dialect.Dialect#limit()
	 */
	@Override
	public LimitClause limit() {
		return LIMIT_CLAUSE;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.data.relational.core.dialect.Dialect#lock()
	 */
	@Override
	public LockClause lock() {
		return LOCK_CLAUSE;
	}
	
	private static final LockClause LOCK_CLAUSE = new LockClause() {
		/*
		 * (non-Javadoc)
		 * @see org.springframework.data.relational.core.dialect.LockClause#getLimit(LockOptions)
		 */
        @Override
        public String getLock(LockOptions lockOptions) {
            return "WITH LOCK";
        }
        
        /*
		 * (non-Javadoc)
		 * @see org.springframework.data.relational.core.dialect.LimitClause#getClausePosition()
		 */
        @Override
        public Position getClausePosition() {
            return Position.AFTER_ORDER_BY;
        }
    };
    
    @Override
    public ArrayColumns getArraySupport() {
        return ArrayColumns.Unsupported.INSTANCE;
    }
}
